/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Structures;

import java.io.File;
import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Mariusz
 */
public class CarrefourProfile extends ReceiptProfile implements Serializable {

    public CarrefourProfile() {
        super("Carrefour");
        this.addTrainingImage(new File("./pliki/carrefour/carrefour_azduze.png"), 'A', 'Z');
        this.addTrainingImage(new File("./pliki/carrefour/carrefour_azmale.png"), 'a', 'z');
        this.addTrainingImage(new File("./pliki/carrefour/carrefour_haszprocent.png"), '#', '%');
        this.addTrainingImage(new File("./pliki/carrefour/carrefour_lewynawiasdwukropek.png"), '(', ':');
        this.addTrainingImage(new File("./pliki/carrefour/carrefour_ostryostry.png"), '<', '>');
    }

    @Override
    public boolean isDefault() {
        return true;
    }
    
    @Override
    public boolean checkIfProduct(String p) {
        return (p.matches(".+[,]{1}.+[ABD]{1}\\s{1}") || 
                p.matches(".+[*]{1}.+[ABD]{1}\\s{1}") ||
                p.matches(".+[ABD]{1}.+[0-9]+.+[ABD]{1}\\s{1}") ||
                p.matches(".+[ABD]{1}.+[*]{1}.+[,]{1}.+[ABD]{1}\\s{1,}") ||
                p.matches(".+[ABD]{1}.+[*]{1}.+[ABD]{1}\\s{1,}")) ? true : false;
    }

    @Override
    public Entry getProduct(String s) {
        Entry product = new Entry();
        
        if(s.matches(".+\\s+[0-9iloszILOSZ,.*]{1,}.\\s{1}.+\\s{1,}")){
            String weight = "";
            String name = "";
            String price = "";

            Matcher matcherWight = Pattern.compile("\\s+[0-9iloszILOSZ,.]{1,}[*]{1}").matcher(s);
            while (matcherWight.find()) {
                weight = matcherWight.group();
            }
            weight = weight.replace('o', '0');
            weight = weight.replace('O', '0');
            weight = weight.replace('z', '2');
            weight = weight.replace('Z', '2');
            weight = weight.replace('S', '8');
            weight = weight.replace('s', '8');
            weight = weight.replace('l', '1');
            weight = weight.replace('i', '1');
            weight = weight.replace('I', '1');
            weight = weight.replaceAll("[^0-9]", "");
            if(weight.length() > 1)
                weight = (weight.length() >= 4) ?
                    new StringBuffer(weight).insert(weight.length() - 3, '.').toString() :
                    new StringBuffer(weight).insert(weight.length() - 2, '.').toString();

            String[] nameAndPrice = s.split("\\s+[0-9iloszILOSZ,.*]{1,}.\\s{1}", 2);
            name = nameAndPrice[0];
            price = nameAndPrice[1];
            price = price.replace('o', '0');
            price = price.replace('O', '0');
            price = price.replace('z', '2');
            price = price.replace('Z', '2');
            price = price.replace('S', '8');
            price = price.replace('s', '8');
            price = price.replace('l', '1');
            price = price.replace('i', '1');
            price = price.replace('I', '1');

            Matcher matcherPrice = Pattern.compile("[*][0-9,.]+.\\s{1,}").matcher(price);
            while (matcherPrice.find()) {
                price = matcherPrice.group();
            }
            price = price.replace('m', '0');

            price = price.replaceAll("[^0-9]", "");
            price = new StringBuffer(price).insert(price.length() - 2, '.').toString();

            product.name = name;
            product.price = Float.parseFloat(price);
            product.quantity = Float.parseFloat(weight);
        }
        
        return product;
    }
}
